﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using ProtoType.DataStorage;
using System.Xml.Linq;
using ProtoType.ProtoTypeService;
namespace ProtoType
{
    public partial class AddControl : PhoneApplicationPage
    {
        private string selectedfid = "";
        AppSettings apps = new AppSettings();
        ProtoTypeServiceClient client = new ProtoTypeServiceClient();
        DataStructureStore dss = new DataStructureStore();

        public AddControl()
        {
            InitializeComponent();
            SelectFieldType.ItemsSource = GetItems();
            SelectFieldType.SelectedIndex = 8;
        }


        private void saveButton_Click(object sender, EventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("fid", out selectedfid))
            {
                if (apps.IsolatedStorageSavingSetting)
                {
                    dss.CreateFormControl(selectedfid, txtName.Text, SelectFieldType.SelectedItem.ToString(), "");
                    NavigationService.Navigate(new Uri("/ManageControlList.xaml?fid=" + selectedfid, UriKind.Relative));
                }
                if (apps.WebServiceSavingSetting)
                {
                    client.CreateFormControlAsync(selectedfid, txtName.Text, SelectFieldType.SelectedItem.ToString(), "");
                    client.CreateFormControlCompleted += new EventHandler<CreateFormControlCompletedEventArgs>(client_CreateFormControlCompleted);
                }
            }
        }

        void client_CreateFormControlCompleted(object sender, CreateFormControlCompletedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/ManageControlList.xaml?fid=" + selectedfid, UriKind.Relative));
        }

        private IList<string> GetItems()
        {
            List<string> items = new List<string>();
            items.Add("Address");
            items.Add("Audio");
            items.Add("Currency");
            items.Add("Date");
            items.Add("Email");
            items.Add("Image");
            items.Add("Number");
            items.Add("Phone");
            items.Add("Text");
            items.Add("Time");
            items.Add("Url");
            items.Add("Video");
            return items;
        }

    }
}